﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsFormsApplication1.API;
using Millionaire;

namespace WindowsFormsApplication1.BLL
{
    public class LuotChoiBLL : BaseBLL, ILuotChoiBLL
    {

        #region ILuotChoiBLL Members

        public bool AddNew(LuotChoi lc)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    ctxt.LuotChois.InsertOnSubmit(lc);
                    ctxt.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        public bool Update(LuotChoi lc)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    LuotChoi edit = ctxt.LuotChois.SingleOrDefault(x => x.MaLuotChoi == lc.MaLuotChoi);
                    if (edit != null)
                    {
                        edit.TongDiem = lc.TongDiem;
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public bool Delete(LuotChoi lc)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    LuotChoi delObj = ctxt.LuotChois.SingleOrDefault(x => x.MaLuotChoi == lc.MaLuotChoi);
                    if (delObj != null)
                    {
                        ctxt.LuotChois.DeleteOnSubmit(delObj);
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        public LuotChoi GetByID(int idLC)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.LuotChois.SingleOrDefault(x => x.MaLuotChoi == idLC);
            }
        }

        public List<LuotChoi> GetAll()
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.LuotChois.ToList();
            }
        }

        public List<LuotChoi> GetByNguoiChoi(int idNguoiChoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.LuotChois.Where(x => x.MaNguoiChoi == idNguoiChoi).ToList();
            }
        }

        #endregion
    }
}
